package com.controller;

import com.bean.Admin;
import com.bean.Enterprise;
import com.bean.Page;
import com.bean.User;
import com.service.AdminService;
import com.service.EnterpriseService;
import com.service.UserService;
import com.service.impl.AdminServiceImpl;
import com.service.impl.EnterpriseServiceImpl;
import com.service.impl.UserServiceImpl;
import com.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

public class AdminServlet extends BaseServlet {

    private AdminService adminService=new AdminServiceImpl();
    private UserService userService=new UserServiceImpl();
    private EnterpriseService enterpriseService=new EnterpriseServiceImpl();

    /**
     * 管理员登录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String adminid=req.getParameter("adminid");
        String password=req.getParameter("password");
        String code=req.getParameter("code");
        //获取验证码
        String token=(String)req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        //删除验证码
        req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);

        if (token != null && token.equalsIgnoreCase(code)) {
            Admin admin=adminService.queryByAdmin(adminid,password);
            if(password.equals(admin.getPassword())){
                req.getSession().setAttribute("admin",admin);
                req.getRequestDispatcher("/page/admin/indexAdmin.jsp").forward(req,resp);
            }else{
                req.setAttribute("mess",password+"验证码错误");
                req.getRequestDispatcher("/page/loginAdmin/login.jsp").forward(req,resp);
            }
        }else {
            System.out.print(code+"验证码错误");
            req.setAttribute("mess",code+"验证码错误");
            req.getRequestDispatcher("/page/loginAdmin/login.jsp").forward(req,resp);
        }
    }

    /**
     * 修改密码
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void modifyPass(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Admin admin= (Admin) req.getSession().getAttribute("admin");
        String adminid=admin.getAdminid();
        if (admin == null) {
            req.getRequestDispatcher("/page/admin/login.jsp").forward(req,resp);
        }
        String mess="";
        String password=req.getParameter("password");
        String again_password=req.getParameter("again_password");
        if(again_password.equals(password)){
            adminService.modifyPass(adminid,password);
            req.setAttribute("mess","密码修改成功！");
            req.getRequestDispatcher("/page/admin/modifyPass.jsp").forward(req,resp);
        }else{
            req.setAttribute("mess","密码修改失败！");
            req.getRequestDispatcher("/page/admin/modifyPass.jsp").forward(req,resp);
        }

    }

    /**
     * 查看企业用户并分页显示
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void queryPageEnterprise(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo= WebUtils.parseInt(req.getParameter("pageNo"),1);
        int pageSize=WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        Page<Enterprise> page=enterpriseService.queryPageEnterprise(pageNo,pageSize);
        page.setUrl("adminServlet?action=queryPageEnterprise");
        req.setAttribute("page",page);
        req.getRequestDispatcher("/page/admin/showEnterprise.jsp").forward(req,resp);
    }

    /**
     * 根据企业编号删除企业
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteEnterprise(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String enterpriseid=req.getParameter("enterpriseid");
        Admin admin= (Admin) req.getSession().getAttribute("admin");
        if(admin==null){
            resp.sendRedirect(req.getContextPath()+"page.admin/login.jsp");
        }
        enterpriseService.delete(enterpriseid);
        req.getRequestDispatcher("adminServlet?action=queryPageEnterprise").forward(req,resp);
    }

    /**
     * 查看普通用户并分页显示
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void queryPageUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int pageNo= WebUtils.parseInt(req.getParameter("pageNo"),1);
        int pageSize=WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        Page<User> page=userService.queryPageUser(pageNo,pageSize);
        page.setUrl("adminServlet?action=queryPageUser");
        req.setAttribute("page",page);
        req.getRequestDispatcher("/page/admin/showUser.jsp").forward(req,resp);
    }

    /**
     * 根据普通用户编号删除企业
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String userid=req.getParameter("userid");
        Admin admin= (Admin) req.getSession().getAttribute("admin");
        if(admin==null){
            resp.sendRedirect(req.getContextPath()+"page.admin/login.jsp");
        }
        userService.deleteByUserid(userid);
        req.getRequestDispatcher("adminServlet?action=queryPageUser").forward(req,resp);



    }

    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().invalidate();
        resp.sendRedirect(req.getContextPath());
    }
}
